<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>优惠券列表</title>
<jsp:include page="../include/header.jsp"></jsp:include>
<style type="text/css">
table td {
	text-align: center;
}

table th {
	text-align: center;
}
</style>
</head>
<body>
	<div id="wrapper" class="row">

		<div class="col-lg-12">
			<h1 class="page-header">优惠券列表</h1>
		</div>
		<!-- 搜索框 -->
		<div class="title-top col-lg-12">
			<label class="search-label">优惠券类型：</label>
			<div class="search-input">
				<select id="type" class="selectpicker" data-width="210">
					<option value="0">请选择</option>
					<option value="1">购物赠券</option>
					<option value="2">注册赠券</option>
					<option value="3">全场赠券</option>
					<option value="4">会员赠券</option>
					<option value="5">打折赠券</option>

				</select>
			</div>
			<label class="search-label">优惠券ID：</label><input type="text" id="id"
				value="" class="form-control search-input width200" maxlength="21"
				placeholder="优惠券的ID">
			<button class="btn btn-success search-btn" onclick="query();">确定</button>
			<button class="btn btn-success search-btn" onclick="reset();">重置</button>
			<button type="button" class="btn btn-success pull-right margin-lr"
				onclick="toAdd()">
				<span class="fa fa-plus"></span> 添加优惠券
			</button>
			<button onclick="doExcel();"
				class="btn btn-success pull-right margin-lr">导出</button>

			<div class="clearfix"></div>
		</div>
		<div class="col-lg-12 title-top">
			<div class="col-lg-12">
				<div>
					<div class="text-muted single-line-text pull-left">
						共 <font color="#428bca" id="dataCount">0</font> 条记录
					</div>


				</div>
			</div>
			<div class="table-responsive panel panel-default">
				<!-- style="width:2520px;  overflow:scroll;"滚动条 -->
				<table id="dataTable"
					class="table table-striped table-hover table-bordered"
					style="overflow: scroll;">
					<thead>
						<tr>
							<th width="120px">优惠券ID</th>
							<th width="200px">优惠券标题</th>
							<th width="100px">优惠券类型</th>
							<th width="80px">发行总量</th>
							<th width="100px">每人限领数量</th>
							<th width="100px">消费门槛</th>
							<th width="80px">面值</th>
							<th width="60px">折扣</th>
							<th width="200px">创建时间</th>
							<th width="150px">生效时间</th>
							<th width="150px">失效时间</th>
							<th width="100px">有效天数</th>
							<th width="160px">操作</th>
						</tr>
					</thead>
					<tbody id="dataList"></tbody>
				</table>
			</div>
			<div class="col-lg-12">
				<ul id="dataPagination" class="pagination-sm pull-right"></ul>
			</div>
		</div>
	</div>

	<jsp:include page="../include/javascripts.jsp"></jsp:include>
	<!-- 异步加载下一页数据后，用模板渲染 -->
	<script type="text/html" id="dataTpl">
	{{each list as item}}
	<tr id="item{{item.id}}" data-id="{{item.id}}">
		<td>{{item.id}}</td>
		<td>{{item.title}}</td>
		<td>{{item.type | flagTransform:1,'购物赠券',2,'注册赠券',3,'全场赠券',4,'会员赠券',5,'打折赠券'}}</td>			
		<td>{{item.count}}</td>
		<td>{{item.limitcount}}</td>
		{{if item.type==5}}
			<td></td>  
		{{else }}           
			 <td>{{item.uselimitmoney}}</td>   
		{{/if}}
        	
        {{if item.type==5}}
			<td></td>  
		{{else }}           
			 <td>{{item.values}}</td>   
		{{/if}}
      	{{if item.type==5}}
			<td>{{item.discount}}</td>
		{{else }}           
			 <td></td>     
		{{/if}}
  <td>{{item.createtime }}</td>
  {{if item.validstarttime!=null}}   
  <td>{{item.validstarttime }}</td> 
  {{else}}
  <td>{{item.validstarttime }}</td> 
  {{/if}}
{{if item.validendtime!=null}}   
  <td>{{item.validendtime }}</td> 
  {{else}}
  <td>{{item.validendtime }}</td> 
  {{/if}}
     <td>{{item.validday}}</td> 
			   <td>
				<a href="javascript:toEdit('{{item.id}}');"><span class="btn">编辑</span></a>
				/
				<a href="javascript:dele('{{item.id}}');"><span class="btn">删除</span></a>
			   </td>
	</tr>
	{{/each}}
</script>
	<script type="text/javascript">
		var dataPaginator;
		$(query);
		function query() {
			var params = {
				pageSize : 10,
				type : $("#type").val(),
				id : $("#id").val()
			};
			if (dataPaginator) {
				dataPaginator.destroy();
			}
			dataPaginator = Kit.pagination("#dataPagination",
					"${BASE_PATH}/pref/page", params, function(result) {
						//设置显示最新的数据数量
						$("#dataCount").html(result.object.totalRow);
						
						//根据模板渲染数据并填充
						$("#dataList").empty().append(
								template("dataTpl", result.object));
					});

		}

		var addDialog;
		function toAdd(p, c, a) {

			addDialog = Kit.dialog("添加", "${BASE_PATH}/pref/toAdd", {
				closable : false
			}).open();

		}

		var editDialog;
		function toEdit(id) {

			editDialog = Kit.dialog("修改", "${BASE_PATH}/pref/toEdit?id=" + id,
					{
						closable : true
					}).open();
		}

		/* 编辑后定位到原位置 */
		function onEditSuccess(newObject) {
			$("#item" + newObject.id).replaceWith(template("dataTpl", {
				"list" : [ newObject ]
			}));
		}

		function dele(id) {
			Kit.confirm("提示", "确定要删除请吗？", function(ref) {
				$.post("${BASE_PATH}/pref/dele", {
					'id' : id
				}, function(result) {
					$("#item" + id).remove();

				});
			});
		}

		function Online(storeid) {
			Kit.confirm("提示", "确定要发起上线申请吗？", function(ref) {
				$.post("${BASE_PATH}/dict/store/Online", {
					'storeid' : storeid
				}, function(result) {
					if (result.flag == 0) {
						onEditSuccess(result.object);
					} else {
						Kit.alert("操作失败");
						return;
					}

				});
			});
		}

		function doExcel() {
			var type = $("#type").val(), id = $("#id").val();

			window.location.href = "${BASE_PATH}/pref/doExcel?type=" + type
					+ "&id=" + id;
		}
		function reset() {
			$("#id").val("");
			$("#type").val("0");
			$("#type").trigger("change");
		}
	</script>
</body>
</html>